import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index.js'
Vue.use(VueRouter)
import Index from '../views/Index.vue'
const routes = [
  {
    path: '/',
    component: Index,
    children:[{
      path:'/',
      redirect:'/home'
    },{
      path:'/home',
      name:'Home',
      component:()=>import('../views/Home.vue')
    },{
      path:'/starclud',
      name:'StarClud',
      component:()=>import('../views/StarClud.vue')
    },{
      path:'/order',
      name:'Order',
      component:()=>import('../views/Order.vue')
    },{
      path:'/me',
      name:'Me',
      component:()=>import('../views/Me.vue')
    }]
  },
  {
    path:'/address',
    name:'Address',
    component:()=>import('../views/Address.vue')
  },
  {
    path:'/addAddress',
    name:'AddAddress',
    component:()=>import('../views/AddAddress.vue')
  },
  {
    path:'/addressSelect',
    name:'AddressSelect',
    component:()=>import('../views/AddressSelect.vue')
  },
  {
    path:'/city',
    name:'City',
    component:()=>import('../views/City.vue')
  },
  {
    path:'/feikuai',
    name:'FeiKuai',
    component:()=>import('../views/FeiKuai.vue')
  },
  {
    path:'/storeInfo',
    name:'StoreInfo',
    component:()=>import('../views/StoreInfo.vue')
  },
  {
    path:'/login',
    name:'Login',
    component:()=>import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//设置路由守卫，如果去往的页面不是登录页面和首页以及用户信息已经存在，则跳转到登录界面
router.beforeEach((to,from,next)=>{
  if(to.name==='Login'||to.name==='Home'||store.getters.userInfo !=null){
    next();
  }else{
    next({name:'Login'});
  }
})
export default router
